package cn.maiyar.web.url.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.maiyar.web.url.enums.SizeEnum;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import cn.maiyar.web.url.UrlProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.net.URL;

/**
 * 路径服务实现
 *
 * @author c.c
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UrlService {

    private final UrlProperties properties;

    /**
     * 获取图片根链接
     *
     * @return
     */
    public String getImage() {
        URL image = properties.getImage();
        if (image == null) {
            return "";
        }
        return image.toString();
    }

    /**
     * 获取图片链接
     *
     * @param url 可多图 “,”隔开
     * @return
     */
    public String getImage(String url) {
        return this.urlPath(properties.getImage(), url);
    }

    /**
     * 获取带有规格的图片信息
     *
     * @param path
     * @param sizeEnum
     * @return
     */
    public String getImage(String path, SizeEnum sizeEnum) {
        String imageUrl = this.urlPath(properties.getImage(), path);
        if (StrUtil.isEmpty(imageUrl)) {
            return "";
        }
        String size = sizeEnum.getSize();
        String[] urls = imageUrl.split(StringPool.COMMA);
        StringBuilder builder = new StringBuilder();
        for (String url : urls) {
            String suffix = FileUtil.getSuffix(url);
            if (StrUtil.isEmpty(suffix)) {
                builder.append(url).append(size).append(StringPool.COMMA);
            } else {
                builder.append(url.replace(StringPool.DOT + suffix, size + StringPool.DOT + suffix)).append(StringPool.COMMA);
            }
        }
        imageUrl = builder.toString();
        if (imageUrl.endsWith(StringPool.COMMA)) {
            imageUrl = imageUrl.substring(0, imageUrl.length() - 1);
        }
        return imageUrl;
    }

    /**
     * 获取项目根域名
     *
     * @return
     */
    public String rootUrl() {
        URL root = properties.getRoot();
        if (root == null) {
            return "";
        }
        return root.toString();
    }

    /**
     * 获取项目资源链接
     *
     * @param resource
     * @return
     */
    public String rootUrl(String resource) {
        return urlPath(properties.getRoot(), resource);
    }

    /**
     * 获取静态资源根域名
     *
     * @return
     */
    public String staticUrl() {
        URL statics = properties.getStatics();
        if (statics == null) {
            return "";
        }
        return statics.toString();
    }

    /**
     * 获取静态资源链接
     *
     * @param resource
     * @return
     */
    public String staticUrl(String resource) {
        return urlPath(properties.getStatics(), resource);
    }

    /**
     * 转换URL
     *
     * @param url
     * @param path
     * @return
     */
    private String urlPath(URL url, String path) {
        if (url == null || StrUtil.isEmpty(path)) {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        String[] split = path.split(StringPool.COMMA);
        for (String pat : split) {
            if (pat.startsWith("http://") || pat.startsWith("https://")) {
                builder.append(pat).append(StringPool.COMMA);
                continue;
            }
            String urls = this.prefixPath(url.getPath(), pat);
            int port = url.getPort();
            if (port == url.getDefaultPort()) {
                port = -1;
            }
            try {
                builder.append(new URL(url.getProtocol(), url.getHost(), port, urls).toString()).append(StringPool.COMMA);
            } catch (Exception e) {
                log.error("url error!", e);
                e.printStackTrace();
            }
        }
        String urlPath = builder.toString();
        if (urlPath.endsWith(StringPool.COMMA)) {
            urlPath = urlPath.substring(0, urlPath.length() - 1);
        }
        return urlPath;
    }

    /**
     * 过滤"/"
     *
     * @param url
     * @param path
     * @return
     */
    private String prefixPath(String url, String path) {
        StringBuilder sb = new StringBuilder();
        if (path != null && !path.startsWith(StringPool.SLASH)) {
            path = StringPool.SLASH + path;
        }
        if (StrUtil.isEmpty(url)) {
            sb.append(path);
            return sb.toString();
        }
        if (StrUtil.isNotEmpty(url)) {
            if (path != null && path.startsWith(StringPool.SLASH) && url.endsWith(StringPool.SLASH)) {
                sb.append(url).append(path.substring(1));
            } else {
                sb.append(url).append(path);
            }
        }
        return sb.toString();
    }
}
